package com.yd.app.service.mobile.messageService;

import com.yd.app.mapper.MessageMapper;
import com.yd.app.mapper.UserMapper;
import com.yd.app.service.common.BaseService;
import com.yd.domain.business.Message;
import com.yd.domain.business.UserMsg;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.MessageParam;
import com.yd.domain.searchParam.MessageReturnParam;
import com.yd.domain.sys.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author zy
 * @create 2018-03-13 9:47
 * @desc
 **/
@Service
public class MMessageService extends BaseService{
    @Autowired
    private MessageMapper messagemapper;
    @Autowired
    private UserMapper userMapper;
    /**
     * @Author: zy
     * @Desc: 录入交流信息
     * @Date:  2018/3/13
     * @Param
     */
    @Transactional
    public void  addMessage(Message message){
        messagemapper.addMessage(message);
        //添加是否含最新消息
        messagemapper.addOrUpdate(new UserMsg(message.getToken(),message.getPublishUser(),message.getReceiveUser(),true));
    }
    /**
     * @Author: zy
     * @Desc: 交流信息修改
     * @Date:  2018/3/13
     * @Param
     */
    public void updateMessage(Message message){
        messagemapper.updateMessage(message);
    }
    /**
     * @Author: zy
     * @Desc: 交流信息删除
     * @Date:  2018/3/13
     * @Param
     */
    public void deleteMessage(String id){
        messagemapper.deleteMessage(id);
    }
    /**
     * @Author: zy
     * @Desc:  分页查询交流信息
     * @Date:  2018/3/13
     * @Param
     */
    public PageModel getMessageList(MessageParam messageParam, int pageIndex, int pageSize){
        PageModel pm=new PageModel();
        List<Message> list=new ArrayList<>();
        list=messagemapper.searchMessageList(messageParam,getStartRow(pageIndex,pageSize),pageSize);
        int count=0;
        if(pageIndex==1){
            //查询总条数
            count=messagemapper.searchMessageCount(messageParam);
            pm.setCount(count);
        }
        pm.setList(list);
        return pm;
    }


    /**
     * @Author: zy
     * @Desc: 批量删除
     * @Date:  2018/3/13
     * @Param
     */
    public void deleteAll(Message message){
        messagemapper.deleteAll(message);
    }

    /**
     * 查询我的聊天列表
     * @param userId
     * @return
     */
    public List<MessageReturnParam> getMessageForList(String userId) {
        List<MessageReturnParam>list=messagemapper.getMessageForList(userId);
        for(MessageReturnParam mrp:list){
           Message msg= messagemapper.getLastMessage(getToken(mrp.getPublishUser(),userId));
           mrp.setLastMessage(msg);
        }
        return list;
    }
    /**
     * 查询我的聊天列表
     * @param userId
     * @return
     */
    public List<MessageReturnParam> getMessageForListWithManager(String userId) {
        List<MessageReturnParam>mesList=new ArrayList<>();
        List<MessageReturnParam>listNotRead=messagemapper.getMessageForList(userId);
        List<User>userList=userMapper.getUserListAll(userId);
        for(User u:userList){
            MessageReturnParam mrp=new MessageReturnParam();
            mrp.setPublishUser(u.getId());
            mrp.setRealName(u.getRealName());
            mrp.setToken(getToken(userId,u.getId()));
            getCountFromMRPList(listNotRead,mrp.getToken(),mrp,userId);
            mesList.add(mrp);
        }
        for(MessageReturnParam mrp:mesList){

            Message msg= messagemapper.getLastMessage(getToken(mrp.getPublishUser(),userId));
            mrp.setLastMessage(msg);
        }
        return mesList;
    }
    public void getCountFromMRPList(List<MessageReturnParam>listNotRead,String token, MessageReturnParam mrp,String userId){
        for(MessageReturnParam mrp_:listNotRead){
            if(mrp_.getToken().endsWith(token)){
                mrp.setCount(mrp_.getCount());
                Message msg= messagemapper.getLastMessage(getToken(mrp.getPublishUser(),userId));
                mrp.setLastMessage(msg);
            }
        }
    }

    public PageModel getMessageTalkForList(String token, int pageIndex, int pageSize) {
        PageModel pm=new PageModel();
        List<Message> list=new ArrayList<>();
        list=messagemapper.searchMessageTalkList(token,getStartRow(pageIndex,pageSize),pageSize);
        int count=0;
        if(pageIndex==1){
            //查询总条数
            count=messagemapper.searchMessageTalkCount(token);
            pm.setCount(count);
        }
        Collections.sort(list, new Comparator<Message>() {
            @Override
            public int compare(Message o1, Message o2) {
                if(o1.getPublishTime().getTime()-o2.getPublishTime().getTime()>0){
                    return 1;
                }else if(o1.getPublishTime().getTime()-o2.getPublishTime().getTime()<0){
                    return -1;
                }else{
                    return 0;
                }
            }
        });
        pm.setList(list);
        return pm;
    }

    //获取我的未读信息数量
    public int getNoReadMessageCount(String userId) {
        int count=messagemapper.getNoReadMessageCount(userId);
        return count;
    }

    //更新已读状态
    public void updateMessageIfRead(String receiveUsreId,String token,boolean b) {
        messagemapper.updateMessageIfRead(receiveUsreId,token,b);
    }

    //删除交谈信息
    public void deleteTalk(String token) {
        messagemapper.deleteTalk(token);
    }
    /**
     * @Author: zy
     * @Desc:获取客服人员
     * @Date:  2018/4/9
     * @Param
     */
    public List<User> getAllCustomService(){
        List<User> list= new ArrayList<>();
        list=messagemapper.getAllCustomService();
        return list;
    }

    //获取是否存在最新消息
    public Boolean getIfMsg(String token,String receiveUser) {
        UserMsg um=messagemapper.getIfMsg(token,receiveUser);
        if(um!=null){
            return um.getIfMsg()!=null? um.getIfMsg() :false;
        }else{
            return  false;
        }
    }

    //获取最新消息未读消息
    @Transactional
    public List<Message> lastMsg(String token, String receiveUser) {
        List<Message>  list=messagemapper.getLastMsg(token,receiveUser);
        if(list!=null){
            messagemapper.updateMessageIfReadWithReceiveUser(token,receiveUser);
            messagemapper.updateIfMsgWithReceiveUser(token,receiveUser);
        }
        return list;
    }

}
